package fr.liglab.adele.icasa.house.device.impl;

import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Provides;

import fr.liglab.adele.icasa.device.DeviceListener;
import fr.liglab.adele.icasa.device.presence.AudioSource;

@Component
@Provides
@Instantiate
public class SumulatedAudioSourceImpl implements AudioSource, Runnable {

	Thread curr = null;
	PipedOutputStream os = new PipedOutputStream();
	PipedInputStream pis = null;

	public String getSerialNumber() {
		// TODO Auto-generated method stub
		return null;
	}

	public void addListener(DeviceListener listener) {
		// TODO Auto-generated method stub

	}

	public void removeListener(DeviceListener listener) {
		// TODO Auto-generated method stub

	}

	public InputStream getStream() {
		// TODO Auto-generated method stub
		if (pis == null) {
			try {
				pis=new PipedInputStream(os);
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return pis;
	}

	public void play() {
		// TODO Auto-generated method stub
//		if (curr == null) {
//			curr = new Thread(this);
//			curr.start();
//		}
	}

	public void stop() {
		// TODO Auto-generated method stub
		if (curr != null) {
			curr.interrupt();
		}

	}

	public void run() {
		// TODO Auto-generated method stub
		try {
			System.out.println("Writing..");
			os.write(6);
			os.close();
			//pis=null;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
